package com.itboye.ihomebank.login;

import java.util.Observable;
import java.util.Observer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.ihomebank.KeyFragment;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.PersonDataBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.controller.LoginController;
import com.itboye.ihomebank.controller.LoginedState;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 登录
 * 
 */
public class LoginActivity extends BaseOtherActivity implements Observer {
	ImageView close_icon;// 返回图片
	TextView add_shap_title_tv;// 标题
	EditText login_pass;// 密码
	EditText login_username;// 账号
	TextView login_ok;// 登录
	TextView login_forget;// 忘记密码
	TextView login_register;// 手机注册
	ImageView login_three;// 第三方登录

	private String phone;
	private String password;

	private Context context;
	private UserPresenter userPresenter;
	public static String LOGINEXIT = "com.itboye.ihomebank.exit";// 登录界面推出指令

	ToggleButton login_tb;// 密码可见

	@Override
	public void onClick(View v) {
		Intent intetn = null;
		switch (v.getId()) {
		case R.id.close_icon:// 返回
			finish();
			break;
		case R.id.login_ok:// 登录
			phone = login_username.getText().toString();
			password = login_pass.getText().toString();
			if (TextUtils.isEmpty(phone)) {

				ByAlert.alert("请输入手机号");
			} else if (TextUtils.isEmpty(password)) {
				ByAlert.alert("请输入密码");
			} else {
				showProgressDialog("登录中,请稍后", true);
				SPUtils.put(LoginActivity.this, null, SPContants.USERNAME,
						phone);
				SPUtils.put(LoginActivity.this, null, SPContants.PASSWORD,
						password);
				login();
			}

			break;
		case R.id.login_forget:// 忘记密码
			intetn = new Intent(LoginActivity.this, PasswordActivityOne.class);
			startActivity(intetn);
			break;
		case R.id.login_register:// 手机注册
			intetn = new Intent(LoginActivity.this, RegisterActivityOne.class);
			startActivity(intetn);
			break;
		case R.id.login_three:// 第三方登录

			break;
		case R.id.login_tb:// 密码可见
			if (login_tb.isChecked()) {
				login_pass
						.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			} else {
				login_pass.setInputType(InputType.TYPE_CLASS_TEXT
						| InputType.TYPE_TEXT_VARIATION_PASSWORD);

			}

			break;

		default:
			break;
		}

	}

	/**
	 * 登录
	 */
	public void login() {
		userPresenter = new UserPresenter(this);
		userPresenter.login(phone, password);
	}

	BroadcastReceiver emailBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			LoginActivity.this.finish();
		};
	};

	protected void onDestroy() {
		super.onDestroy();
		// unregisterReceiver(emailBroadcastReceiver);
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		try {
			closeProgressDialog();
		} catch (Exception e) {
		}
		if (entity != null) {
			if (!entity.getCode().equals(0)) {
				if (entity.getEventType() == UserPresenter.user_success) {
					PersonDataBean bean = (PersonDataBean) entity.getData();
					if (bean != null) {
						Log.i("result", bean.toString());

						SPUtils.put(LoginActivity.this, null,
								SPContants.USERNAME, bean.getUsername());
						SPUtils.put(LoginActivity.this, null,
								SPContants.MOBILE, bean.getMobile());
						if (bean.getMobile() != null) {
							if (bean.getMobile().equals("")) {
								SPUtils.put(LoginActivity.this, null,
										SPContants.IS_BIND_PHONE, false);
							} else {
								SPUtils.put(LoginActivity.this, null,
										SPContants.IS_BIND_PHONE, true);
							}
						} else {
							SPUtils.put(LoginActivity.this, null,
									SPContants.IS_BIND_PHONE, false);
						}
						SPUtils.put(LoginActivity.this, null,
								SPContants.IS_LOGINED, true);
						SPUtils.put(LoginActivity.this, null, SPContants.EMAIL,
								bean.getEmail());
						SPUtils.put(LoginActivity.this, null, SPContants.NICK,
								bean.getNickname());
						SPUtils.put(LoginActivity.this, null,
								SPContants.PASSWORD, bean.getPassword());
						SPUtils.put(LoginActivity.this, null,
								SPContants.USER_ID, bean.getUid());
						SPUtils.put(LoginActivity.this, null,
								SPContants.AVATAR_URL, bean.getHead());
						SPUtils.put(LoginActivity.this, null, SPContants.SEX,
								bean.getSex());// 性别
						SPUtils.put(LoginActivity.this, null,
								SPContants.IdentityValidate,// '实名身份认证【0未认证,1已认证,2待审核】'
								bean.getIdentityValidate());
						SPUtils.put(LoginActivity.this, null,
								SPContants.BrokerValidate,
								bean.getBrokerValidate());// 经纪人认证【0未认证,1已认证,2待审核】
						SPUtils.put(LoginActivity.this, null,
								SPContants.ScienerUsername,// 绑定科技侠账号，科技侠账号,为空未绑定
								bean.getScienerUsername());
						SPUtils.put(LoginActivity.this, null,
								SPContants.IdNumBer,// 身份证号码
								bean.getIdnumber());
						SPUtils.put(LoginActivity.this, null,
								SPContants.WalletPass,//钱包支付密码 
								bean.getWalletPassword());
						SPUtils.put(LoginActivity.this, null,
								SPContants.RealName, bean.getRealname());// 身份证号码
						SPUtils.put(this, null, SPContants.BaichuanUID,
								bean.getBaichuanUid());// 阿里百川即时通讯 的uid

						if (bean.getWalletPassword() != null) {// 支付密码
							if (bean.getWalletPassword().equals("")) {
								SPUtils.put(LoginActivity.this, null,
										SPContants.IS_ZHIFU_PASS, false);
							} else {
								SPUtils.put(LoginActivity.this, null,
										SPContants.IS_ZHIFU_PASS, true);
							}
						} else {
							SPUtils.put(LoginActivity.this, null,
									SPContants.IS_ZHIFU_PASS, false);
						}
						try {

							LoginActivity.this.finish();
						} catch (Exception e) {
							// TODO: handle exception
						}

						LoginController.setLoginState(new LoginedState());
						// 发送广播通知更改头像
						Intent intent2 = new Intent("changeavart");
						sendBroadcast(intent2);
						KeyFragment.getInstance().panduan(bean.getUid());
					}
				} else if (entity.getEventType() == UserPresenter.user_fail) {
					ByAlert.alert(entity.getData());
				}
			}
		}
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_login;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		login_username.setText("");// 测试使用
		login_pass.setText("");

		// 短信登录发送过来的广播
		IntentFilter filter1 = new IntentFilter(LOGINEXIT);
		registerReceiver(emailBroadcastReceiver, filter1);
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	};

	/*
	 * public void login() {
	 * 
	 * MyJsonRequest<PersonDataBean> request = new
	 * MyJsonRequest.Builder<PersonDataBean>()
	 * .apiVer("100").typeKey("BY_User_login") .param("username",
	 * phone).param("password", password) .requestListener(new
	 * XRequestListener<PersonDataBean>() {
	 * 
	 * @Override public void onResponse(PersonDataBean response) {
	 * DebugLog.v("result", response.toString()); Log.i("result",
	 * response.toString()); SPUtils.put(LoginActivity.this, null,
	 * SPContants.WX_Openid, response.getWxopenid());
	 * SPUtils.put(LoginActivity.this, null, SPContants.USERNAME,
	 * response.getUsername()); SPUtils.put(LoginActivity.this, null,
	 * SPContants.MOBILE, response.getMobile()); SPUtils.put(LoginActivity.this,
	 * null, SPContants.IS_LOGINED, true); SPUtils.put(LoginActivity.this, null,
	 * SPContants.EMAIL, response.getEmail()); SPUtils.put(LoginActivity.this,
	 * null, SPContants.NICK, response.getNickname());
	 * SPUtils.put(LoginActivity.this, null, SPContants.PASSWORD,
	 * response.getPassword()); SPUtils.put(LoginActivity.this, null,
	 * SPContants.USER_ID, response.getUid()); SPUtils.put(LoginActivity.this,
	 * null, SPContants.AVATAR_URL, response.getHead());
	 * SPUtils.put(LoginActivity.this, null, SPContants.AQ_NUM,
	 * response.getAqCount()); SPUtils.put(LoginActivity.this, null,
	 * SPContants.SEX, response.getSex()); LoginActivity.this.finish();
	 * LoginController.setLoginState(new LoginedState()); // 发送广播通知更改头像(还未实现)
	 * Intent intent2 = new Intent("changeavart"); sendBroadcast(intent2); }
	 * }).errorListener(new XErrorListener() {
	 * 
	 * @Override public void onErrorResponse(Exception exception, int code,
	 * String msg) { ByAlert.alert(msg); // T.showShort(context, "请求错误"); }
	 * }).build(); HttpRequest.getDefaultRequestQueue().add(request); }
	 */
}
